package mobilesmil.data;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;

/**
 * The resource Object that extends {@link SmilResource} and provides functionality for
 * playing an image in a SMIL.
 */
public class SmilImageResource extends SmilResource {
	/**
	 * The Bitmap that is the image from the provided source.
	 */
	private Bitmap mImage;

	/**
	 * 
	 * @param source		Name of file where the image is located.
	 * @param smilRegion	What region should to displayed the image in.
	 * @param beginTime		When should the image be displayed.
	 * @param endTime		When should the image stop being displayed.
	 */
	public SmilImageResource(String source, SmilRegion smilRegion, int beginTime, int endTime) {
		super(source, smilRegion, beginTime, endTime);
		super.setType(TYPE_IMAGE);
		loadImageFromSource();
	}
	
	/**
	 * Sets {@link mImage} to the image located at the source. 
	 * Should only be called when the source of the image has changed.
	 */
	private void loadImageFromSource(){
		mImage = BitmapFactory.decodeFile(SmilResource.ROOT_PATH + super.getSource());
		
		//check to make sure file was decoded
		if(mImage == null){
			mImage = Bitmap.createBitmap(50, 50, Bitmap.Config.RGB_565);
		}
	}
	
	@Override
	public void play(Canvas canvas) {
		Rect r = super.getSmilRegion().getRect();
		canvas.save();
		canvas.clipRect(r);
		canvas.drawBitmap(mImage, r.left, r.top, null);
		canvas.restore();
	}

	@Override
	public void stop(Canvas canvas) {
	}
}
